package br.com.site.model.entity;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.OneToOne;
import javax.persistence.PrimaryKeyJoinColumn;

import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;

import br.com.site.model.dao.DAOException;
import br.com.site.model.dao.DAOFactory;
import br.com.site.model.dao.PerguntaDAO;

@Entity
public class Pergunta implements CrudBaseEntity<Pergunta, Long>, Auditoria {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	@Id
	@GeneratedValue
	private Long id;

	@ManyToOne(targetEntity = br.com.site.model.entity.Estande.class, fetch = FetchType.LAZY)
	private Estande estandePergunta;

	@OneToOne(mappedBy = "pergunta", cascade = { CascadeType.REMOVE })
	@PrimaryKeyJoinColumn
	@NotFound(action = NotFoundAction.IGNORE)
	private Resposta resposta;

	@ManyToOne(targetEntity = br.com.site.model.entity.Usuario.class, fetch = FetchType.EAGER)
	private Usuario donoPergunta;

	@Column(columnDefinition="text")
	private String texto = "";

	/* Auditoria */
	@Column(name = "loginUsuario", nullable = false)
	private String loginUsuario;

	@Column(name = "dtCriacao", nullable = false)
	private Date dtCriacao;

	@Column(name = "dtModificacao", nullable = false)
	private Date dtModificacao;

	@Column(name = "ativo", nullable = false)
	private boolean ativo;

	public String getLoginUsuario() {
		return loginUsuario;
	}

	public void setLoginUsuario(String loginUsuario) {
		this.loginUsuario = loginUsuario;
	}

	public Date getDtCriacao() {
		return dtCriacao;
	}

	public void setDtCriacao(Date dtCriacao) {
		this.dtCriacao = dtCriacao;
	}

	public Date getDtModificacao() {
		return dtModificacao;
	}

	public void setDtModificacao(Date dtModificacao) {
		this.dtModificacao = dtModificacao;
	}

	public boolean isAtivo() {
		return ativo;
	}

	public void setAtivo(boolean ativo) {
		this.ativo = ativo;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setTexto(String texto) {
		if (texto != null)
			this.texto = texto;
	}

	public Usuario getDonoPergunta() {
		return donoPergunta;
	}

	public void setDonoPergunta(Usuario donoPergunta) {
		this.donoPergunta = donoPergunta;
	}

	public String getTexto() {
		return texto;
	}

	public Estande getEstandePergunta() {
		return estandePergunta;
	}

	public void setEstandePergunta(Estande estandePergunta) {
		this.estandePergunta = estandePergunta;
	}

	public Resposta getResposta() {
		return resposta;
	}

	public void setResposta(Resposta resposta) {
		this.resposta = resposta;
	}

	public boolean equals(Object obj) {
		boolean bool = false;
		if (obj instanceof Pergunta)
			bool = hashCode() == ((Pergunta) obj).hashCode();
		return bool;
	}

	@Override
	public void remove() throws BusinessException {
		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			PerguntaDAO dao = factory.getDao(PerguntaDAO.class);
			dao.remove(this.id);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}

	}

	@Override
	public void save() throws BusinessException {

		DAOFactory factory = DAOFactory.getInstance();
		try {
			factory.txBegin();
			PerguntaDAO dao = factory.getDao(PerguntaDAO.class);
			dao.save(this);

			factory.txCommit();
		} catch (DAOException e) {
			factory.txRollback();
			throw new BusinessException(e);
		} finally {
			factory.shutdown();
		}
	}

	public static Pergunta find(Long oid) {
		DAOFactory factory = DAOFactory.getInstance();
		PerguntaDAO dao = factory.getDao(PerguntaDAO.class);

		return dao.find(oid);
	}

	public static List<Pergunta> find() {
		DAOFactory factory = DAOFactory.getInstance();
		PerguntaDAO dao = factory.getDao(PerguntaDAO.class);

		return dao.find();
	}

	public static List<Pergunta> findByCriteria(Pergunta avisoMural) {
		DAOFactory factory = DAOFactory.getInstance();
		PerguntaDAO dao = factory.getDao(PerguntaDAO.class);

		return dao.findByCriteria(avisoMural);
	}

	public static List<Pergunta> find(int first, int max) {
		DAOFactory factory = DAOFactory.getInstance();
		PerguntaDAO dao = factory.getDao(PerguntaDAO.class);

		return dao.find(first, max);
	}

	public static List<Pergunta> findByCriteria(Pergunta avisoMural, int first,
			int max) {
		DAOFactory factory = DAOFactory.getInstance();
		PerguntaDAO dao = factory.getDao(PerguntaDAO.class);

		return dao.findByCriteria(avisoMural, first, max);

	}

}